package com.example.ranl_safeapi.Service.Impl;

import com.example.ranl_safeapi.Dao.StudentDao;
import com.example.ranl_safeapi.Entity.PageResult;
import com.example.ranl_safeapi.Entity.Student;
import com.example.ranl_safeapi.Service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public PageResult<Student> findByPrefix(Integer pageNum, Integer pageSize, String namePrefix) {
        //创建自定义的实体类来存放总数和当前页的内容
        PageResult<Student> pageResult = new PageResult<>();
        //启动pagehelper开启内存分页查询
        PageHelper.startPage(pageNum, pageSize);

        //查询数据
        List<Student> students = studentDao.findByPrefix(namePrefix);

        PageInfo<Student> pageInfo = new PageInfo<>(students);

        //将分页信息储存到pageResult
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setList(pageInfo.getList());

        return pageResult;
    }

    @Override
    public PageResult<Student> findByBirth(Integer pageNum, Integer pageSize, Date startday, Date finalday) {
        //创建自定义的实体类来存放总数和当前页的内容
        PageResult<Student> pageResult = new PageResult<>();
        //启动pagehelper开启内存分页查询
        PageHelper.startPage(pageNum, pageSize);

        //查询数据
        List<Student> students = studentDao.findByBirth(startday,finalday);

        PageInfo<Student> pageInfo = new PageInfo<>(students);

        //将分页信息储存到pageResult
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setList(pageInfo.getList());

        return pageResult;
    }
}
